package com.txl.cn.spark06

import java.sql.DriverManager

import com.mysql.jdbc.Connection
import org.apache.spark.rdd.JdbcRDD
import org.apache.spark.{SparkConf, SparkContext}

/**
  * Created by txl on 2018/1/4.
  */
object JDBCDemo {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf()
      .setMaster("local")
      .setAppName(this.getClass.getName)
    val sc = new SparkContext(conf)
    val getConn = () => {
      DriverManager.getConnection("jdbc:mysql://localhost:3306/scott?characterEncoding=utf-8", "root", "123456")
    }
    val jdbc=new JdbcRDD(
      sc,
      getConn,
      "select * from salgrade where grade >= ? and grade <?",
      1,
      4,
      1,
      res=>{
        val grade = res.getInt(1)
        val low = res.getInt(2)
        val hig = res.getInt(3)
        (grade, low, hig)
      }
    )
    jdbc.collect().foreach(println)
  }
}
